package com.guaoran.algorithm.queue;

import java.util.*;

/**
 * @Auth gucheng
 * @Desc 推荐结果打散
 * @From 快手
 * @Date 2020/12/28 下午1:08
 */
public class RecommendedResultsAreScattered {
    /**
     * v1,v2,v3,p4,p5,p6,v7,v8,p9,v10
     * 根据集合进行结果打散，即每两个v，就要有一个p,
     */
    public static void main(String[] args) {
        List<String> picAndVideo = Arrays.asList("v1","v2","v3","p4","p5","p6","v7","v8","v10","v11","v12");
        System.out.println(getRecommendedResults(picAndVideo,2));
    }

    public static List<String> getRecommendedResults(List<String> picAndVideo,int maxInterval){
        List<String> result = new ArrayList<>();
        if(picAndVideo == null || picAndVideo.size() == 0){
            return result;
        }

        Queue<String> videoQueue = new LinkedList<>();
        Queue<String> picQueue = new LinkedList<>();
        for (String str : picAndVideo) {
            if(isPic(str)){
                picQueue.add(str);
            }else {
                videoQueue.add(str);
            }
        }

        int size = 0;
        // 每添加 maxInterval 个 v 就增加一个 p
        while(!videoQueue.isEmpty()){
            if(size == maxInterval && !picQueue.isEmpty()){
                result.add(picQueue.poll());
                size = 0;
            }else{
                result.add(videoQueue.poll());
                size ++;
            }
        }
        // 如果 v 添加了满足了，如果 p 还有值则再添加一个 p
        if(size == maxInterval && !picQueue.isEmpty()){
            result.add(picQueue.poll());
        }

        return result;
    }
    private static boolean isPic(String str){
        return str.contains("p");
    }
}
